//
//  decode_enc_pb_reply.hpp
// ~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2013-2014 Razvan Coca (razvan dot coca at  gmail dot com)
// This is mostly generated code. Generator is distributed under GPL. Edit it to suit your needs.
// 


#ifndef decode_enc_pb_reply_hpp
#define decode_enc_pb_reply_hpp
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>

#include "patterns/command.hpp"
#include "patterns/handlers.hpp"

#include "logging/logging.hpp"

namespace pcmd=patterns::command;
namespace phdl=patterns::handlers;
namespace ba=boost::asio;
namespace bs=boost::system;
namespace bt=boost::posix_time;

#include "components/defs/util_types.hpp"
#include "components/defs/client_processor.hpp"
#include "components/encrypt/aes_service.hpp"
namespace components{
namespace protocol{



class decode_enc_pb_reply :public boost::enable_shared_from_this<decode_enc_pb_reply>, private boost::noncopyable
{
private:
    std::string name_;

    ba::io_service& ios_;

    crypto_buf_t& iv_;

    crypto_buf_t& key_;

    stats_message_client_processor& message_processor_;


    std::string serialize_buf_;

    decode_enc_pb_reply (ba::io_service& ios, crypto_buf_t& iv, crypto_buf_t& key, stats_message_client_processor& message_processor) ;

    boost::shared_ptr<components::encrypt::aes_service> aes_service_;

public:
    typedef boost::shared_ptr<decode_enc_pb_reply> pointer;

    bool cancel () ;

    static boost::shared_ptr<decode_enc_pb_reply> create (ba::io_service& ios, crypto_buf_t& iv, crypto_buf_t& key, stats_message_client_processor& message_processor) ;

    virtual  ~decode_enc_pb_reply () ;

 template<typename Handler >
      bool exec(bytes_vector_t& recv_buf,std::string& decrypt_buf_, Handler& handler) {
        try{
            LOG(INFO)<<name_<<"::"<<__FUNCTION__;
            serialize_buf_=std::string(recv_buf.begin(),recv_buf.end());
            aes_service_->enc_key(iv_,key_);
            aes_service_->dec_key(iv_,key_);
            bool ret=aes_service_->exec(components::encrypt::aes_service::decrypt_op,serialize_buf_,boost::ref(decrypt_buf_),handler);

            return ret;
        }
        catch(std::exception &e){
           LOG(WARNING)<<name_<<"::"<<__FUNCTION__<<":"<<e.what();
           cancel();
           return false;
        }
        
    }
};

}//end namespace protocol
}//end namespace components

#endif /*#ifdef decode_enc_pb_reply_hpp*/
